package com.tjbklx1.redis.chap01;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class App01 {

	@Test
	public void getSetString() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String value = redis.get("hello");
			System.out.println(value);

			redis.set("hello", "world");

			value = redis.get("hello");
			System.out.println(value);

			redis.del("hello");

			value = redis.get("hello");
			System.out.println(value);

		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}

	}

	@Test
	public void list() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String key = "list-key";
			
			redis.del(key);
			
			printList(redis, key);
			redis.rpush("list-key", "item");
			redis.rpush("list-key", "item2");
			redis.rpush("list-key", "item");
			printList(redis, key);

			System.out.println(redis.lindex(key, 0));
			redis.lpop(key);
			printList(redis, key);
		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}

	}

	private void printList(Jedis redis, String key) {
		List<String> resultList = redis.lrange(key, 0, -1);
		if (resultList != null && resultList.size() > 0) {
			System.out.println("############################");
			for (int i = 0; i < resultList.size(); i++) {
				System.out.println(i + " : " + resultList.get(i));
			}
			System.out.println("############################");
		}
	}

	@Test
	public void set() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String key = "set-key";
			
			redis.del(key);
			
			
			redis.sadd(key, "item");
			redis.sadd(key, "item2");
			redis.sadd(key, "item3");
			redis.sadd(key, "item");
			printSet(redis, key);
			System.out.println(redis.sismember(key, "item4"));
			System.out.println(redis.sismember(key, "item"));

			System.out.println(redis.srem(key, "item"));
			System.out.println(redis.srem(key, "item2"));
			printSet(redis, key);

		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}
	}

	private void printSet(Jedis redis, String key) {
		Set<String> resultSet = redis.smembers(key);
		System.out.println("############################");
		for (String str : resultSet) {
			System.out.println(str);
		}
		System.out.println("############################");
	}

	@Test
	public void hash() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String key = "hash-key";
			
			redis.del(key);
			
			redis.hset(key, "No1", "item");
			redis.hset(key, "No2", "item2");
			redis.hset(key, "No3", "item3");
			redis.hset(key, "No4", "item");
			printHash(redis, key);
			System.out.println(redis.hdel(key, "No4"));
			System.out.println(redis.hget(key, "No1"));
			printHash(redis, key);

		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}
	}

	private void printHash(Jedis redis, String key) {
		Map<String, String> map = redis.hgetAll(key);
		Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> entry = it.next();
			System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
		}
	}

	@Test
	public void zset() {

		Jedis redis = null;
		try {
			JedisPool pool = new JedisPool("localhost", 6379);
			redis = pool.getResource();
			String key = "zset-key";
			
			redis.del(key);
			
			redis.zadd(key, 100, "item");
			redis.zadd(key, 80, "item2");
			redis.zadd(key, 200, "item3");
			redis.zadd(key, 1000, "item4");
			printZSet(redis, key);
			System.out.println(redis.zrem(key, "item"));
			System.out.println(redis.zrem(key, "item4"));
			printZSet(redis, key);

		} catch (Exception e) {
			e.printStackTrace();
			if (redis != null) {
				redis.close();
			}
			e.printStackTrace();
		} finally {
			if (redis != null) {
				redis.close();
			}
		}
	}

	private void printZSet(Jedis redis, String key) {
		Set<String> set = redis.zrangeByScore(key, 0, 1000);
		System.out.println("############################");
		for (String str : set) {
			System.out.println(str);
		}
		System.out.println("############################");
	}
}
